package team.nmsg.ge.system.init;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;


import team.nmsg.ge.system.bean.ServerStatus;
import team.nmsg.ge.system.interf.IBusinessLogAddit;
import team.nmsg.ge.system.util.ServerParams;
import team.nmsg.ge.system.util.SysDBHelper;



public class CommonDefine {
	
	private static final Log logger = LogFactory.getLog(CommonDefine.class);
	
	public static final String STR_ENTERPRISEID = "[EnterpriseID]";
	
	public static void initCommonDefine(ApplicationContext appContext){
		SysDBHelper dbHelper = appContext.getBean(SysDBHelper.class);
		try {
			initBusinessLogType();//业务日志添加器
			initNPSServerStatus(dbHelper);
		} catch (Exception e) {
			logger.error("" ,e );
		}
		
	}

	// 系统业务日志类型
		public static Map<Integer, String> logTypeToDesc = new HashMap<Integer, String>();
		public static Map<Integer, Map<String, IBusinessLogAddit>> logAddits = new HashMap<Integer, Map<String, IBusinessLogAddit>>();
		private static final String BLOG_CONFIG = ServerParams.BASE_CONF_PATH
				+ "BusinessLogAddit.xml";

		public static void initBusinessLogType() {
			File file = new File(BLOG_CONFIG);
			SAXReader reader = new SAXReader();
			Document doc = null;
			try {
				doc = reader.read(file);
			} catch (Exception e) {
				return;
			}
			Element root = doc.getRootElement();
			List<Element> modules = root.elements();
			for (Element module : modules) {
				Integer moduleId = Integer.parseInt(module.attributeValue("id"));
				String moduleName = module.attributeValue("name");
				logTypeToDesc.put(moduleId, moduleName);
				List<Element> addits = module.elements();
				Map<String, IBusinessLogAddit> tmpMNameToAddit = new HashMap<String, IBusinessLogAddit>();
				for (Element addit : addits) {
					String methodName = addit.attributeValue("methodName");
					String additClass = addit.attributeValue("additClass");
					if (additClass != null && additClass.length() > 0
							&& methodName != null) {
						try {
							IBusinessLogAddit additObj = (IBusinessLogAddit) Class
									.forName(additClass).newInstance();
							tmpMNameToAddit.put(methodName, additObj);
						} catch (Exception e) {
							logger.error("", e);
						}
					}
				}
				logAddits.put(moduleId, tmpMNameToAddit);
			}
		}

	
	public static String PUBLISH_KEY = "publish_key";
	public static String DOWNLOAD_KEY = "download_key";
	public static void initNPSServerStatus( SysDBHelper dbHelper ){
		Session hSession = null;
		try {
			hSession = dbHelper.openHSession();			
			Criteria criteria = hSession.createCriteria(ServerStatus.class);
			criteria.add(Restrictions.eq("skey", PUBLISH_KEY));
			List existKey = criteria.list();
			if( existKey == null || existKey.size()==0 ){
				hSession.createCriteria(ServerStatus.class);
				ServerStatus ss = new ServerStatus();
				ss.setSkey(PUBLISH_KEY);
				ss.setSvalue("true");
				hSession.save(ss);
				hSession.flush();		
			}
			
			criteria = hSession.createCriteria(ServerStatus.class);
			criteria.add(Restrictions.eq("skey", DOWNLOAD_KEY));
			existKey = criteria.list();
			if( existKey == null || existKey.size()==0 ){
					hSession.createCriteria(ServerStatus.class);
					ServerStatus ss = new ServerStatus();
					ss.setSkey(DOWNLOAD_KEY);
					ss.setSvalue("true");
					hSession.save(ss);
					hSession.flush();		
				}
				
		} catch (Exception e) {
			logger.error("" ,e );
		}finally{
			hSession.close();
		}
		
	}
	
}
